package com.linghang.wusthelper.spider.jwc;

import com.linghang.wusthelper.entity.primary.CourseInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * @author origin
 * 课程表爬虫
 */
public class CurriculumSpider implements PageProcessor {

    private List<CourseInfo> courseInfoList;

    private String lock = "";

    private Site site = Site.me()
            .setRetryTimes(3)
            .setTimeOut(3000)
            .addHeader("Connection", "keep-alive")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Encoding", "gzip,deflate,sdch")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36")
            .addHeader("Cache-Control","no-cache")
            .addHeader("Host","jwxt.wust.edu.cn")
            .setDomain("jwxt.wust.edu.cn");

    public CurriculumSpider(String JSESSIONID,String SERVERID,List<CourseInfo> courseInfoList,String lock) {
        site.addCookie("JSESSIONID",JSESSIONID);
        site.addCookie("SERVERID",SERVERID);
        this.courseInfoList = courseInfoList;
        this.lock = lock;
    }

    @Override
    public void process(Page page) {
        for (int row = 1; row <= 6;row ++){
            for (int line = 1;line <= 7;line++){
                String path = "//*[@id=\""+row+"-"+line+"-";
                String names = page.getHtml().xpath(path+"1\"]/text()").get();

                // 如果输入了未来的学期数，就会不停的响应
                // 所以这里要判断 如果是为null 就直接结束整个循环
                if (names == null){
//                    row = 6;
//                    line = 7;
//                    continue;
                    break;
                }


                names = names.replaceAll(" ","");
                if (names != null && !names.equals("")){
                    int classNums = names.split(",").length;
                    String weekday = String.valueOf(line);
                    String classTime = String.valueOf(row);
                    for (int i = 1; i <= classNums;i++){
                        Selectable s = page.getHtml().regex("<div id=\""+row+"-"+line+"-2\" style=\"display: none;\">(.{3,}?)</div>").replace("\\n\\s+","");
                        String className = s.regex("(.*?)<br>(.*?)<br>(.*?)<nobr><br>(.*?)<br>",1).all().get(i-1).replaceAll("&nbsp;","");
                        String classNo = s.regex("(.*?)<br>(.*?)<br>(.*?)<nobr><br>(.*?)<br>",2).all().get(i-1);
                        String classRoom = s.regex("(.*?)<br>(.*?)<br>(.*?)<nobr><br>(.*?)<br>",4).all().get(i-1);
                        String str = s.regex("(.*?)<br>(.*?)<br>(.*?)<nobr><br>(.*?)<br>",3).all().get(i-1);
                        int index = str.indexOf(" ");
                        int index2 = str.indexOf("[");
                        String teacher = str.substring(0,index);
                        String week = str.substring(index+1,index2-1);
                        String[] weeks = week.split(",");
                        for (String tmp:weeks){
                            if (tmp.contains("-")){
                                int index3 = tmp.indexOf("-");
                                String startWeek = tmp.substring(0,index3);
                                String endWeek = tmp.substring(index3+1);
                                CourseInfo courseInfo = new CourseInfo(className,classNo,teacher,startWeek,endWeek,classTime,classRoom,weekday);
                                System.out.println(courseInfo);
                                courseInfoList.add(courseInfo);
                            }
                        }
                    }
                }
            }
        }
        synchronized (lock){
            lock.notify();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
